package com.example.JcGQS.handler;

import com.example.JcGQS.exception.CustomException;
import com.example.JcGQS.exception.TokenException;
import com.example.JcGQS.response.ResultUtil;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.client.HttpServerErrorException;

import java.sql.SQLSyntaxErrorException;

/**
 * 全局异常处理类
 *
 * @author 末日
 * @date 2023/11/21
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * sql异常
     **/
    @ExceptionHandler(SQLSyntaxErrorException.class)
    public ResultUtil sqlSyntaxErrorException(SQLSyntaxErrorException e) {
        return ResultUtil.fail("501","sql错误："+e.getMessage());
    }


    /**
     * HttpServerErrorException
     **/
    @ExceptionHandler(HttpServerErrorException.class)
    public ResultUtil httpServerErrorException(HttpServerErrorException e) {
        return ResultUtil.fail("503",e.getMessage());
    }
    /**
     * 自定义异常
     **/
    @ExceptionHandler(CustomException.class)
    public ResultUtil bindException(CustomException e) {
        return ResultUtil.fail(e.getMessage());
    }
    /**
     * token异常
     **/
    @ExceptionHandler(TokenException.class)
    public ResultUtil bindException(TokenException e) {
        return ResultUtil.fail("401",e.getMessage());
    }
    /**
     * 所有异常拦截
     **/
    @ExceptionHandler(Exception.class)
    public ResultUtil bindException(Exception e) {
        return ResultUtil.fail("500","参数校验异常" + e.getMessage());
    }
}
